package com.whpost.api.error;
/**
 * 
 *@Title GlobalExceptionHandler.java
 *@description 全局异常处理
 *@time 2018年12月4日 下午3:14:46
 *@author YinZhuoyi
 *@version 1.0
*
 */

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.alibaba.fastjson.JSON;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.tools.ToolBean;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.PrintWriter;

@ControllerAdvice
class GlobalExceptionHandler {

    public static final String DEFAULT_ERROR_VIEW = "error";

    /**
     *
     * @param e
     * @param response
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = com.whpost.api.error.MyException.class)
    @ResponseBody
    public Object jsonErrorHandler(MyException e, HttpServletResponse response) throws Exception {
        e.printStackTrace();
        ResultInfo r = new ResultInfo();
        r.setMsg(e.getMessage());
        r.setCode(Integer.parseInt(e.getCode()));
        r.setData(e.getData());

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(r));
        writer.flush();
        writer.close();
        return null;
    }

    /**
     *
     * @param e
     * @param response
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object defaultErrorHandler(Exception e, HttpServletResponse response) throws Exception {
        e.printStackTrace();
        ToolBean.info("全局异常："+e.toString());
        ResultInfo r = new ResultInfo();
        r.setMsg(e.getMessage());
        r.setCode(500);
        r.setData(null);

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(r));
        writer.flush();
        writer.close();
        return null;
    }

    // 拦截：未登录异常
    @ExceptionHandler(NotLoginException.class)
    @ResponseBody
    public Object handlerException(NotLoginException e,HttpServletResponse response) throws Exception {
        String msg;
        ResultInfo r = new ResultInfo();
        switch (e.getType()) {
            case NotLoginException.NOT_TOKEN:
                msg = "账号已注销，请重新登录";
                break;
            case NotLoginException.INVALID_TOKEN:
                msg = NotLoginException.INVALID_TOKEN_MESSAGE;
                break;
            case NotLoginException.TOKEN_TIMEOUT:
                msg = "Token已过期，请重新登录";
                break;
            case NotLoginException.BE_REPLACED:
                msg = "账号被顶强制下线";
                break;
            case NotLoginException.KICK_OUT:
                msg = "账号被强制踢出，请重新登录";
                break;
            default:
                msg = "当前会话未登录";
        }

        r.setMsg(msg);
        r.setCode(6);
        r.setData(null);

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(r));
        writer.flush();
        writer.close();
        return null;
    }

    @ExceptionHandler(value = NotRoleException.class)
    @ResponseBody
    public Object handleNotRoleException(HttpServletResponse response, NotRoleException e) throws IOException {
        ResultInfo r = new ResultInfo();
        r.setMsg("权限不足，没有对应的角色！");
        r.setCode(403);
        r.setData(null);

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(r));
        writer.flush();
        writer.close();
        return null;
    }

    @ExceptionHandler(value = NotPermissionException.class)
    @ResponseBody
    public Object handleNotPermissionException(HttpServletResponse response, NotPermissionException e) throws IOException {
        ResultInfo r = new ResultInfo();
        r.setMsg("权限不足，没有对应的权限！");
        r.setCode(403);
        r.setData(null);

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(r));
        writer.flush();
        writer.close();
        return null;
    }
}
